#include "g1_heap.hpp"
#include <iostream>

class TestObject
{
    ObjectHeader header;
    void *ref1;
    void *ref2;

public:
    TestObject() : ref1(nullptr), ref2(nullptr)
    {
        header.size = sizeof(TestObject);
        header.is_marked = false;
        header.mark_epoch = 0;

        // 创建一个简单的Klass对象
        static Klass test_klass;
        test_klass.reference_field_count = 2; // 假设这个类有两个引用字段
        header.klass_pointer = &test_klass;
    }

    void set_ref1(void *r1, void *r2)
    {
        ref1 = r1;
        ref2 = r2;
    }
};

int main()
{
    G1Heap *heap = new G1Heap();
    // 分配一些对象
    TestObject *obj1 = static_cast<TestObject *>(heap->allocate(sizeof(TestObject)));
    TestObject *obj2 = static_cast<TestObject *>(heap->allocate(sizeof(TestObject)));
    TestObject *obj3 = static_cast<TestObject *>(heap->allocate(sizeof(TestObject)));
    ;
    // 设置引用关系
    obj1->set_ref1(obj2, obj3);
    obj2->set_ref1(obj3, nullptr);

    // 添加根对象
    heap->add_root(obj1);

    // 进行一次GC
    heap->collect_garbage();

    std::cout << "GC completed." << std::endl;

    delete heap;
    return 0; // 程序结束后，堆会自动被销毁，因此不需要手动释放obj1, obj2, obj3等对象的内存
}